﻿using System;
using System.Collections.Generic;
using l1cs.server.datatables;
using l1cs.server.model.Instance;
using l1cs.server.templates;

namespace l1cs.server.serverpackets
{
    public class S_AuctionBoard : ServerBasePacket
    {
        private static Logger _log = Logger.getLogger( typeof( S_AuctionBoard ).FullName );

        private byte[] _byte = null;

        public S_AuctionBoard( L1NpcInstance board )
        {
            buildPacket( board );
        }

        private void buildPacket( L1NpcInstance board )
        {
            List<int> houseList = new List<int>();
            int houseId = 0;
            int count = 0;
            int[] id = null;
            String[] name = null;
            int[] area = null;
            int[] month = null;
            int[] day = null;
            int[] price = null;

            foreach ( L1AuctionBoard ab in AuctionBoardTable.getInstance().getAuctionBoardTableList() )
            {
                houseId = ab.getHouseId();
                if ( board.getX() == 33421 && board.getY() == 32823 )
                { // 競売掲示板(ギラン)
                    if ( houseId >= 262145 && houseId <= 262189 )
                    {
                        houseList.Add( houseId );
                        count++;
                    }
                }
                else if ( board.getX() == 33585 && board.getY() == 33235 )
                { // 競売掲示板(ハイネ)
                    if ( houseId >= 327681 && houseId <= 327691 )
                    {
                        houseList.Add( houseId );
                        count++;
                    }
                }
                else if ( board.getX() == 33959 && board.getY() == 33253 )
                { // 競売掲示板(アデン)
                    if ( houseId >= 458753 && houseId <= 458819 )
                    {
                        houseList.Add( houseId );
                        count++;
                    }
                }
                else if ( board.getX() == 32611 && board.getY() == 32775 )
                { // 競売掲示板(グルーディン)
                    if ( houseId >= 524289 && houseId <= 524294 )
                    {
                        houseList.Add( houseId );
                        count++;
                    }
                }
            }

            id = new int[count];
            name = new String[count];
            area = new int[count];
            month = new int[count];
            day = new int[count];
            price = new int[count];

            for ( int i = 0; i < count; ++i )
            {
                L1AuctionBoard auctionBoard =
                    AuctionBoardTable.getInstance().getAuctionBoardTable( houseList[i] );
                id[i] = auctionBoard.getHouseId();
                name[i] = auctionBoard.getHouseName();
                area[i] = auctionBoard.getHouseArea();
                DateTime deadline = (DateTime)auctionBoard.getDeadline();
                month[i] = deadline.Month;
                day[i] = deadline.Day;
                price[i] = auctionBoard.getPrice();
            }

            writeC( Opcodes.S_OPCODE_HOUSELIST );
            writeD( board.getId() );
            writeH( count ); // レコード数
            for ( int i = 0; i < count; ++i )
            {
                writeD( id[i] ); // アジトの番号
                writeS( name[i] ); // アジトの名前
                writeH( area[i] ); // アジトの広さ
                writeC( month[i] ); // 締切月
                writeC( day[i] ); // 締切日
                writeD( price[i] ); // 現在の入札価格
            }
        }

        override
        public byte[] getContent()
        {
            if ( _byte == null )
            {
                _byte = getBytes();
            }
            return _byte;
        }

        override
        public String getType()
        {
            return "[S] S_AuctionBoard";
        }
    }
}
